<!DOCTYPE HTML>
<html>
<head>
  <title>Test for DOMCursor</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
"use strict";

SimpleTest.waitForExplicitFinish();

var reqserv = SpecialPowers.getDOMRequestService();
ok("createRequest" in reqserv, "appears to be a service");

var req;
var lastContinue = false;

var index = 0;

function next() {
  if (index < tests.length) {
    ok(true, "Begin test");
    tests[index++]();
  } else {
    ok(true, "All done");
    SimpleTest.finish();
  }
}

var tests = [
  function() {
    // create a cursor, test its interface and its initial state
    req = reqserv.createCursor(window, function() {
      if (lastContinue) {
        reqserv.fireDone(req);
      } else {
        reqserv.fireSuccess(req, "next result")
      }
    });
    ok("result" in req, "cursor has result");
    ok("error" in req, "cursor has error");
    ok("onsuccess" in req, "cursor has onsuccess");
    ok("onerror" in req, "cursor has onerror");
    ok("readyState" in req, "cursor has readyState");
    ok("done" in req, "cursor has finished");
    ok("continue" in req, "cursor has continue");
    ok(!("then" in req), "cursor should not have a then method");

    is(req.readyState, "pending", "readyState is pending");
    is(req.result, undefined, "result is undefined");
    is(req.onsuccess, null, "onsuccess is null");
    is(req.onerror, null, "onerror is null");
    next();
  },
  function() {
    // fire success
    req.onsuccess = function(e) {
      ok(e, "got success event");
      is(e.type, "success", "correct type during success");
      is(e.target, req, "correct target during success");
      is(req.readyState, "done", "correct readyState after success");
      is(req.error, null, "correct error after success");
      is(req.result, "my result", "correct result after success");
      is(req.done, false, "cursor is not done after continue")
      next();
    }
    reqserv.fireSuccess(req, "my result");
  },
  function() {
    // continue
    req.onsuccess = function(e) {
      ok(e, "got success event after continue");
      is(e.type, "success", "correct type during continue");
      is(e.target, req, "correct target during continue");
      is(req.readyState, "done", "correct readyState after continue");
      is(req.error, null, "correct error after continue");
      is(req.result, "next result", "correct result after continue");
      is(req.done, false, "cursor is not done after continue")
      next();
    }
    req.continue();
  },
  function() {
    // FireDone
    req.onsuccess = function(e) {
      ok(e, "got success event after continue");
      is(e.type, "success", "correct type during continue");
      is(e.target, req, "correct target during continue");
      is(req.readyState, "done", "correct readyState after continue");
      is(req.error, null, "correct error after continue");
      is(req.result, undefined, "no result after last continue");
      is(req.done, true, "cursor is done after last continue")
      try {
        req.continue();
        ok(false, "continue when cursor is done should fail");
      } catch (e) {
        ok(true, "continue when cursor is done should fail");
      }

      next();
    }
    lastContinue = true;
    req.continue();
  },
  function() {
    // fire error
    req = reqserv.createCursor(window, function(){});
    req.onerror = function(e) {
      ok(e, "got success event");
      is(e.type, "error", "correct type during error");
      is(e.target, req, "correct target during error");
      is(req.readyState, "done", "correct readyState after error");
      is(req.error.name, "error msg", "correct error after error");
      is(req.result, undefined, "correct result after error");
      try {
        req.continue();
        ok(false, "continue while in an error state should fail");
      } catch (e) {
        ok(true, "continue while in an error state should fail");
      }

      next();
    }
    reqserv.fireError(req, "error msg");
  }
];

next();

</script>
</pre>
</body>
</html>
